package top.mccat.raven.factory;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import top.mccat.raven.utils.utils.ColorUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: ConfigFactory
 * @Description: init YAML File.
 * @Author: Raven
 * @Date: 2021/7/7
 * @Version: 1.0
 */
public class ConfigFactory {
    private JavaPlugin plugin;
    private File configFile,itemFile;
    FileConfiguration config,item;
    public ConfigFactory(JavaPlugin plugin){
        this.plugin = plugin;
        configFile = new File(plugin.getDataFolder(),"config.yml");
        itemFile = new File(plugin.getDataFolder(),"item.yml");
        config = YamlConfiguration.loadConfiguration(configFile);
        item = YamlConfiguration.loadConfiguration(itemFile);
    }

    public void initConfigFileData(){
        if(!plugin.getDataFolder().exists() && !plugin.getDataFolder().isDirectory()){
            plugin.saveDefaultConfig();
            plugin.saveResource("item.yml",false);
        }else {
            plugin.getLogger().info("已有文件数据");
        }
    }

    public void reloadFileData(){
        config = YamlConfiguration.loadConfiguration(configFile);
        item = YamlConfiguration.loadConfiguration(itemFile);
        if(!configFile.exists() || !itemFile.exists()){
            try {
                config.save(configFile);
                item.save(itemFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String getColorLevelLore(String key){
        return config.get(key).toString();
    }

    public int[] getLevelArray(String key){
        int[] chanceArray = new int[30];
        Object[] damageChance;
        damageChance = config.getList(key).toArray();
        for(int i=0;i<damageChance.length;i++){
            chanceArray[i] = Integer.parseInt(damageChance[i].toString());
        }
        return chanceArray;
    }

    public Object[] getStrengthItemName(String key){
        return item.getList(key).toArray();
    }

    public float getLevelDamage(String key){
        return Float.parseFloat(item.get(key).toString());
    }

    public FileConfiguration getItem() {
        return item;
    }

    public FileConfiguration getConfig() {
        return config;
    }

    private boolean haveFile(File file){
        if(file == null){
            return false;
        }else {
            return true;
        }
    }
}
